#ifndef QTIMRECEIVERDLG_H
#define QTIMRECEIVERDLG_H

#include <QDialog>
#include <QUrl>
#include "../Core/msgbus_interface.h"
#include "../Core/msgbus_handlerbase.hpp"
#include "../Biz/MsgBusParam/IMContactParam.h"

#include <boost/noncopyable.hpp>
#include <string>
#include <map>
#include <boost/shared_ptr.hpp>

using namespace NetMsgBus;

namespace Ui {
    class QTIMReceiverDlg;
}
class QListWidgetItem;
class QTimer;
class IMSecurityCodeDlg;

class QTIMReceiverDlg : public QDialog, public MsgHandler<QTIMReceiverDlg>
{
    Q_OBJECT

public:
    explicit QTIMReceiverDlg(QWidget *parent = 0);
    ~QTIMReceiverDlg();

private:
    Ui::QTIMReceiverDlg *ui;
signals:
    void notifyLoginOKUid(std::string);
    void receiveMsgs(MsgBusParam param);
    void receiveContacts(MsgBusParam param);


    void receiveAddContactAddedByOther(MsgBusParam param);
    void receiveAddContactAskAddRequest(MsgBusParam param);
    void receiveAddContactDeniedByOther(MsgBusParam param);
    void receiveAddContactAgreeAddByOther(MsgBusParam param);
    void receiveSubscriptionMsg(MsgBusParam param);
    void receiveNeedAuthCode(MsgBusParam param);
    void receiveAuthCodeFailed(std::string session);
    void receiveAuthCodeOK(std::string session);
    void receiveLoginFailed(QString);
    void receiveLoginOK(MsgBusParam param);
    void receiveLogout();
    void receiveLoginError(QString);
    void receiveVerifyUpdate(MsgBusParam param);
    void receiveVerifyUpdateForAddContact(MsgBusParam param);

    void newactivity();
    void receiveHistoryData(MsgBusParam param);
    void openHistoryView();
    void immainerror();


public slots:
    void OnAnchorClicked(QUrl url);
    void OnAddContactClicked();
    void OnContactListItemClicked(QListWidgetItem* pitem);
    void OnSendMsgClicked();
    void OnSendFileClicked();
    void OnFilterTagChanged(QString tagname);
    void OnSendAuthCode(std::string session, std::string authcode);
    void OnCancelAuthCode(std::string session);
    void OnDoLogin(std::string userid, std::string pass, int loginstate);
    void OnDoLogout();
    void OnDoQuit();
    void OnDoVerifyUser(std::string uid, int type = VERIFYTYPE_CHECKSENDMSG);
    void OnDoAddContact(std::string uid);
    void OnDoAgreeAdd(std::string uid);
    void OnDoAgreeAddAndAddHim(std::string uid);
    void OnDoDenyAdd(std::string uid);
    void OnSetDestUser(QString userid);

    void OnPicTransferFinished(MsgBusParam param);
    void OnOfflineImageNotify(MsgBusParam param);

private slots:
    void OnReceiveMsgs(MsgBusParam param);
    void OnReceiveContacts(MsgBusParam param);
    void OnReceiveAddContactAddedByOther(MsgBusParam param);
    void OnReceiveAddContactAskAddRequest(MsgBusParam param);
    void OnReceiveAddContactDeniedByOther(MsgBusParam param);
    void OnReceiveAddContactAgreeAddByOther(MsgBusParam param);
    void OnReceiveSubscriptionMsg(MsgBusParam param);
    void OnReceiveNeedAuthCode(MsgBusParam param);
    void OnReceiveAuthCodeFailed(std::string session);
    void OnReceiveAuthCodeOK(std::string session);
    //void OnReceiveLogout();
    //void OnReceiveLoginError();
    void OnReceiveLoginOK(MsgBusParam param);
    void OnReceiveVerifyUpdate(MsgBusParam param);
    void OnReceiveVerifyUpdateForAddContact(MsgBusParam param);
    //void OnLoginFailed();
    //void OnLoginOK();
    void OnHealthCheck();
    void OnGlobalErrClear();

    void OnViewHistoryClicked();


    void on_btnSendPic_clicked();

public:
    void NotifyQuitToWWMain();
    void NotifyReadyToWWMain();
    void SetWWMainUniqueFlag(const std::string& unique);
    void InitMsgHandler();
    void CleanDataOnLogout();
    void GetAllContacts(QStringList& allcontacts);
    std::string GetLoginUserId()
    {
        return m_loginuid;
    }

private:
    void AppendMsgsAndScroll(const QString& htmlStr, bool issendbyother);
    void UpdateRecentContactToFile();
    void ReadRecentContactFromFile();
    void FillContactDataToContactListWidget(wwbiz::ContactContainerT contacts);
    bool OnReqQuit(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnTextMsg(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMMainError(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMContactsReady(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMAddContactAddedByOther(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMAddContactAskAddRequest(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMAddContactDeniedByOther(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMAddContactAgreeAddByOther(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMSubscriptionMsg(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMNeedAuthCode(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMAuthCodeOK(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMAuthCodeFailed(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMLoginFailed(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMLoginOK(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMLogout(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMLoginError(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMVerifyUserUpdate(const std::string& msgid, MsgBusParam& param, bool& is_continue);
    bool OnIMVerifyUserUpdateForAddContact(const std::string& msgid, MsgBusParam& param, bool& is_continue);

    bool OnIMExportBinMsgsRsp(const std::string& msgid, MsgBusParam& param, bool& is_continue);


private:

    //bool m_real_close;
    std::string m_wwmain_unique;
    bool  m_running;
    QTimer *phealth_timer;
    QTimer *pglobal_err_clear_timer;
    std::map<std::string, wwbiz::ContactContainerT> m_all_group2contacts;
    wwbiz::ContactContainerT  m_all_contacts;
    std::map<std::string, boost::shared_ptr<IMSecurityCodeDlg> > m_all_authcodedlg;

    bool m_is_inloginsession;
    std::string m_loginuid;
    bool m_waitingverify_sendmsg;
    bool m_waitingverify_sendfile;

};

#endif // QTIMRECEIVERDLG_H
